<?php
	include('mysql_config.php');
	$pdo = new PDO($dsn,$user,$password);
	$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
	date_default_timezone_set('PRC');

	$stmt = $pdo->prepare('select dss.*,m.amount from dx_sms_send as dss inner join dx_member as m on m.id = dss.uid where dss.status = 0 order by dss.id desc');
	$stmt->execute();
	$time = time();

    while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
    	$bool = false;

    	//费用不足
    	if($row['sms_cost'] > $row['amount']){
    		continue;
    	}

    	if($row['type'] == '1'){
    		//定时发送
    		if($time >= $row['send_time']){
    			$sql_sms_send_update = "UPDATE dx_sms_send SET status='1' WHERE id={$row['id']}";
                $cycle_send_txt = ''; 	
    			$pdo->query($sql_sms_send_update);
    			$bool = true;
    		}
    	}else{
    		//循环发送短信
    		if(!empty($row['send_start_time']) && !empty($time >= $row['send_end_time'])){
	    		if($time <= $row['send_start_time'] || $time >= $row['send_end_time']){
	    			//不在时间范围内
	    			continue;
	    		}
    		}

    		if(!empty($row['cycle_text'])){
    			$cycle_text = json_decode($row['cycle_text'],1);
    			$year = date('Y',$time);
    			$month = date('m',$time);
    			$day = date('d',$time);
    			$week = date('w');

    			switch ($cycle_text['cyctype']) {
    				case 'y':
    					$string_plan =  "{$year}-{$cycle_text['plan']['month']}-{$cycle_text['plan']['day']} {$cycle_text['plan']['hour']}:{$cycle_text['plan']['minute']}:0";
    					$string_plan_time = strtotime($string_plan);
    					break;
    				case 'm':
    					$string_plan =  "{$year}-{$month}-{$cycle_text['plan']['day']} {$cycle_text['plan']['hour']}:{$cycle_text['plan']['minute']}:0";
    					$string_plan_time = strtotime($string_plan);
    					break;
    				case 'w':
    					$string_plan = "{$year}-{$month}-{$day} {$cycle_text['plan']['hour']}:{$cycle_text['plan']['minute']}:0";

    					if($week == $cycle_text['plan']['week']){
    						$string_plan_time = strtotime($string_plan);
    					}else{
    						$string_plan_time = '';
    					}
    					break;
    				case 'd':
    					$string_plan = "{$year}-{$month}-{$day} {$cycle_text['plan']['hour']}:{$cycle_text['plan']['minute']}:0";

    					$string_plan_time = strtotime($string_plan);
    					break;
    			}

    			$start_time = $time-30;
    			$end_time = $time+30;

    			if($string_plan_time != ''){
    				if($start_time<=$string_plan_time && $string_plan_time<=$end_time){
    					$cycle_send_txt_array = json_decode($row['cycle_send_txt'],1);

    					$cycle_send_txt_array[] = $time;

    					$cycle_send_txt = json_encode($cycle_send_txt_array);

    					$bool = true;
    				}
    			}
			}
    	}

    	//更改成功
    	if($bool){
            //费用扣除
    		$sql_sms_send_update = "UPDATE dx_sms_send SET cycle_send_txt='{$cycle_send_txt}',send_time='{$time}' WHERE id={$row['id']}";
			$pdo->query($sql_sms_send_update);
			
            $feiyong['sms_price'] = $row['sms_cost']/$row['sms_count'];
            $feiyong['moble_count'] = $row['sms_count'];
            $text_content = json_encode($feiyong);
            $sql_member_flow ="INSERT INTO dx_member_flow (`type`,`text_content`,`time`,`money`,`uid`) value('-1','{$text_content}','{$time}','{$row['sms_cost']}','{$row['uid']}')";
            $pdo->query($sql_member_flow);

            $row['amount'] -= $row['sms_cost'];

            $cut_feiyong = "UPDATE dx_member SET amount = '{$row['amount']}' WHERE id={$row['uid']}";
            $pdo->query($cut_feiyong);
    	}
	}
?>